package tecpointbenchmark.model.modules;

/**
 * Performing FLOPS and reporting how many loops are made to the MultiThreadFloatModule
 * 
 * @author Linus & Patrik 
 */
public class FloatThread implements Runnable {

    private boolean continueBenchmarking;
    private MultiThreadFloatModule master = null;
    private SingleThreadFloatModule sMaster = null;

    /**
     *
     * @param master The object to report to when 100 loops are done.
     */
    FloatThread(MultiThreadFloatModule master) {
        this.master = master;
    }

    public FloatThread(SingleThreadFloatModule master) {
        this.sMaster = master;
    }

    /**
     * Making the calculation loop 100 times and then report to it's master.
     */
    @Override
    public void run() {

        continueBenchmarking = true;

        float[] f1 = {0f, 0f, 0f, 0f};
        float[] f2 = {1.0123f, 2.9434f, 3.5636f, 4.8491f};
        float[] f3 = {5.3273f, 6.7492f, 7.1943f, 8.4889f};

        while (continueBenchmarking) {
            //as it takes more time to access another object so it will be done
            //in bursts of 100, this will give precision to every 100 FLOP
            for (int i = 0; i < 100; i++) {

                f1[0] = f2[0] + f3[0];
                f1[1] = f2[1] + f3[1];
                f1[2] = f2[2] + f3[2];
                f1[3] = f2[3] + f3[3];

                f1[0] = f2[0] + f3[1];
                f1[1] = f2[1] + f3[2];
                f1[2] = f2[2] + f3[3];
                f1[3] = f2[3] + f3[0];

                f1[0] = f2[0] + f3[2];
                f1[1] = f2[1] + f3[3];
                f1[2] = f2[2] + f3[0];
                f1[3] = f2[3] + f3[1];

                f1[0] = f2[0] + f3[3];
                f1[1] = f2[1] + f3[0];
                f1[2] = f2[2] + f3[1];
                f1[3] = f2[3] + f3[2];
            }
            if (sMaster == null) {
                master.setScore();
            }
            if (master == null) {
                sMaster.setScore();
            }
        }
    }

    /**
     * @param continueBenchmarking the continueBenchmarking to set
     */
    public void setContinueBenchmarking(boolean b) {
        this.continueBenchmarking = b;
    }

    /**
     * @return the boolean var continueBenchmarking
     */
    public boolean isContinueBenchmarking() {
        return continueBenchmarking;
    }
}
